package sg.techstudio.suntec.controller;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import sg.techstudio.suntec.dao.EventContactDao;
import sg.techstudio.suntec.object.AjaxListResponse;
import sg.techstudio.suntec.object.RestEventContactObj;

@Controller
public class CmsContactEventController extends BaseController {
	private static final Log log = LogFactory.getLog(CmsContactEventController.class);
	private static final String PARAM_EVENTCONTACTOBJ = "eventContactObj";
	
	@Override
	protected String getModuleFolder() {
		return "cms/contactevent/";
	}
	
	@Autowired
	private EventContactDao eventContactDao;

	
	@RequestMapping(value = "cms/contactevent/list" + SPRING_EXT)
	@PreAuthorize("hasRole('ROLE_CMS')")
	public String contactevent_list(Model model) {
		
		return goToPageJsp("list");
	}
	
	@ResponseBody
	@RequestMapping(value = "cms/contactevent/ajaxlist" + SPRING_EXT, headers="Accept=application/json")
	@PreAuthorize("hasRole('ROLE_CMS')")
	public AjaxListResponse eventcontact_ajaxlist(Model model
			, String sEcho
			, int iDisplayStart
			, int iDisplayLength
			, String sSearch
			, HttpServletRequest req, HttpServletResponse rep, HttpSession ses) {
		log.debug("eventcontact_ajaxlist() entry");
		
		long recordNo = eventContactDao.countContactEvent(sSearch);
		AjaxListResponse resp = new AjaxListResponse();
		resp.setsEcho(sEcho);
		resp.setiTotalRecords(recordNo);
		resp.setiTotalDisplayRecords(recordNo);
		
		List<RestEventContactObj> list = eventContactDao.getContactEventList(sSearch, iDisplayStart, iDisplayLength);
		List<Map<String, Object>> aaList = new LinkedList<Map<String,Object>>();
		for(RestEventContactObj r: list) {
			Map<String, Object> m = new HashMap<String, Object>();
			m.put("0", r.getExtId());
			m.put("1", r.getType()==1?"Contact":"Event");
			m.put("2", r.getName());
			m.put("3", formatDateColumn(r.getActivateStartDate()));
			m.put("4", formatDateColumn(r.getActivateEndDate()));
			m.put("DT_RowId", r.getId());
			aaList.add(m);
		}
		
		resp.setAaData(aaList);
		log.debug("eventcontact_ajaxlist() exit");
		return resp;
	}
	
	@RequestMapping(value = "cms/contactevent/view" + SPRING_EXT)
	@PreAuthorize("hasRole('ROLE_CMS')")
	public String contactevent_view(Model model
			, Long id) {
		log.debug("contactevent_view() entry");
		
		if(id==null) {
			return contactevent_list(model);
		}
		RestEventContactObj eventContactObj = eventContactDao.getEventContact(id);
		if(eventContactObj==null) {
			return contactevent_list(model);
		}
		model.addAttribute(PARAM_EVENTCONTACTOBJ, eventContactObj);
		
		return goToPageJsp("view");
	}
}
